package top.chukongxiang.project.base.utils;



import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class StringUtils {

    static final String phoneRegx = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
    static final String emailRegx = "^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$";

    /**
     * 去除空白字符
     * @param str
     * @return
     */
    public static String trimWhitespace(String str) {
        if (str == null) {
            return str;
        } else {
            StringBuffer sb = new StringBuffer();
            for(int i = 0; i < str.length(); ++i) {
                char c = str.charAt(i);
                if (c != '\n' && c != '\f' && c != '\r' && c != '\t') {
                    sb.append(c);
                }
            }
            return sb.toString().trim();
        }
    }

    /**
     * 去除空白字符
     * @param strArr
     * @return
     */
    public static List<String> trimWhitespace(String[] strArr) {
        List<String> list = new ArrayList<String>();
        for(int i=0;i<strArr.length;i++){
            String s = trimWhitespace(strArr[i]);
            if(s!=null && !"".equals(s)) {
                list.add(s);
            }
        }
        return list;
    }
    public static List<String> trimWhitespace(List<String> strArr) {
        List<String> list = new ArrayList<String>();
        for(int i=0;i<strArr.size();i++){
            String s = trimWhitespace(strArr.get(i));
            if(s!=null && !"".equals(s)) {
                list.add(s);
            }
        }
        return list;
    }

    /**
     * 是否为空
     * @param str
     * @return
     */
    private static boolean isEmpty(String str){
        if(str==null || str.length()==0 || trimWhitespace(str).length()==0) {
            return true;
        }
        return false;
    }
    public static boolean isEmpty(Object... str){
        return hasEmpty(str);
    }

    public static boolean notEmpty(Object... objs){
        return !isEmpty(objs);
    }
    /**
     * 是否包含空
     * @param obgArray
     * @return
     */
    public static boolean hasEmpty(Object[] obgArray){
        if (obgArray==null) {
            return true;
        }
        for(Object o:obgArray) {
            if((o instanceof String) && isEmpty((String)o)) {
                return true;
            } else if (o==null) {
                return true;
            }
        }
        return false;
    }

    static Random random = new Random();

    /**
     * 随机字符串 指定长度
     * @param length
     * @return
     */
    public static String randomString(int length){
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<length;i++){
            int r = random.nextInt(3);
            Character o = null;
            if (r == 0) {
                o = (char) (random.nextInt(10) + 48);
            } else if(r == 1) {
                o = (char)(random.nextInt(26)+65);
            } else if(r == 2) {
                o = (char) (random.nextInt(26)+97);
            }
            sb.append(o);
        }
        return sb.toString();
    }

    /**
     * 是否是手机号
     * @param phone
     * @return
     */
    public static boolean isPhoneNumber(String phone){
        if(isEmpty(phone) || phone.length()!=11) {
            return false;
        }
        return phone.matches(phoneRegx);
    }

    /**
     * 是否是邮箱
     * @param email
     * @return
     */
    public static boolean isEmail(String email){
        if(isEmpty(email)) {
            return false;
        }
        return email.matches(emailRegx);
    }

    /**
     * 随机数，指定长度
     * @param length
     * @return
     */
    public static String randomInteger(int length){
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<length;i++){
            sb.append(random.nextInt(10));
        }
        return sb.toString();
    }

    /**
     * 是否包含子串
     * @param parent
     * @param child
     * @return
     */
    public static boolean hasString(String[] parent, String child){
        if(parent==null) {
            return false;
        }
        for (String s:parent){
            if(s==null && child==null) {
                return true;
            } else if(s!=null) {
                if(s.equals(child)) {
                    return true;
                }
            } else if(child!=null) {
                if(child.equals(s)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 获取指定字符在父串中的所有位置
     * @param str
     * @param c
     * @return
     */
    public static List<Integer> getCharAtIndex(String str,Character c){

        List<Integer> indexs = new ArrayList<>();
        if(c==null) {
            return indexs;
        }
        for(int i=0;i<str.length();i++){
            if(str.charAt(i)==c) {
                indexs.add(i);
            }
        }
        return indexs;
    }

    public static List<Integer> getCharAtIndex(String str,String c){
        List<Integer> indexs = new ArrayList<>();
        if(isEmpty(c)) {
            return indexs;
        }
        int i = str.indexOf(c);
        if(i>0){
            indexs.add(i);
            List<Integer> charAtIndex = getCharAtIndex(str.substring(i + c.length()), c);
            if(charAtIndex.size()>0) {
                for(Integer j:charAtIndex) {
                    indexs.add(j);
                }
            }
        }
        return indexs;
    }

    public static String replace(String oldString,String target,int pre,int suf){
        StringBuffer sb = new StringBuffer(oldString.substring(0,pre));
        sb.append(target);
        sb.append(oldString.substring(suf));
        return sb.toString();
    }

    public static boolean isChild(String[] parent,String... child){
        for(String s:child){
            if(!hasString(parent,s)) {
                return false;
            }
        }
        return true;
    }

    public static boolean Equals(String s1,String s2){
        if (s1==s2) {
            return true;
        }
        if (s1!=null) {
            if (s1.equals(s2)) {
                return true;
            }
        }
        if (s2!=null) {
            if (s2.equals(s1)) {
                return true;
            }
        }
        return false;
    }

    public static String[] concatString(String prefix,String[] strings,String suffix){
        String[] newStr = new String[strings.length];
        if(isEmpty(prefix)) {
            prefix = "";
        }
        if(isEmpty(suffix)) {
            suffix = "";
        }
        for(int i=0;i<strings.length;i++){
            if(strings[i]!=null){
                newStr[i] = prefix+strings[i]+suffix;
            }
        }
        return newStr;
    }

    public static String replace_ToUppercase(String oldString){
        if (oldString==null) {
            return null;
        }
        StringBuilder sb = new StringBuilder("");
        for(int i=0;i<oldString.length();i++){
            char c = oldString.charAt(i);
            if (c=='_' && i+1<oldString.length()){
                sb.append(Character.toUpperCase(oldString.charAt(i+1)));
            } else {
                sb.append(oldString.charAt(i));
            }
        }
        return sb.toString();
    }

    public static String toLowerCase(String oldString){
        if (oldString==null) {
            return null;
        }
        return oldString.toLowerCase();
    }

    public static String generalParamFieldKey(){
        StringBuilder sb = new StringBuilder("f_");
        sb.append(System.currentTimeMillis());
        sb.append("_");
        sb.append(randomString(3));
        return sb.toString();
    }


}
